/*******************************************************************************
 * Copyright (c) 2011 squid.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *     squid - initial API and implementation
 ******************************************************************************/
package com.google.code.cubeirc.ui;

import lombok.Getter;
import lombok.Setter;

import org.eclipse.swt.widgets.Composite;

import com.google.code.cubeirc.base.BaseTab;
import com.google.code.cubeirc.common.Utils;
import com.google.code.cubeirc.connection.Connection;
import com.google.code.cubeirc.editor.EditorManager;
import com.google.code.cubeirc.editor.HistoryAdapter;
import com.google.code.cubeirc.queue.MessageQueueEnum;
import com.google.code.cubeirc.queue.MessageQueueEvent;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.events.KeyAdapter;
import org.eclipse.swt.events.KeyEvent;

public class IrcConsoleForm extends BaseTab {
	private StyledText stOutput;
	private StyledText tInput;
	
	@Getter @Setter
	private EditorManager editormanager;

	
	public IrcConsoleForm(Composite composite, int style, String name) {
		super(composite, style, name);
	
		jbInit();
	}
	private void jbInit() {
		setLayout(new FormLayout());
		
		this.stOutput = new StyledText(this, SWT.BORDER | SWT.V_SCROLL | SWT.WRAP | SWT.MULTI | SWT.READ_ONLY);
		FormData fd_stOutput = new FormData();
		fd_stOutput.top = new FormAttachment(0);
		fd_stOutput.left = new FormAttachment(0);
		this.stOutput.setLayoutData(fd_stOutput);
		
		this.tInput = new StyledText(this, SWT.BORDER | SWT.SINGLE);
		this.tInput.addKeyListener(new HistoryAdapter());
		this.tInput.addKeyListener(new KeyAdapter() {
			@Override
			public void keyPressed(KeyEvent arg0) {
				if (arg0.keyCode == 13)
				{
					Connection.getIrcclient().sendRawLine(tInput.getText());
					tInput.setText("");
				}
			}
		});
		fd_stOutput.right = new FormAttachment(this.tInput, 0, SWT.RIGHT);
		fd_stOutput.bottom = new FormAttachment(this.tInput, -6);
		FormData fd_tInput = new FormData();
		fd_tInput.left = new FormAttachment(0);
		fd_tInput.right = new FormAttachment(100);
		fd_tInput.bottom = new FormAttachment(100);
		this.tInput.setLayoutData(fd_tInput);

		setEditormanager(new EditorManager("EDITOR_IRC_Console", stOutput));
		new EditorManager("EDITOR_IRC_Input", tInput);
	}

	@Override
	protected void checkSubclass() {
		// Disable the check that prevents subclassing of SWT components
	}
	
	@Override
	public void actionPerformed(MessageQueueEvent e) {
		
		if (e.getMsgtype() == MessageQueueEnum.IRC_LOGDATA)
		{
			String value = "";
			if (e.getData().toString().startsWith(">>>"))
			value = String.format("[%s] [color_yellow]%s", Utils.getCurrentTime(), e.getData().toString());
			else
				value =String.format("[%s] %s", Utils.getCurrentTime(), e.getData().toString());
			
			getEditormanager().addText(value);
			
		}
		super.actionPerformed(e);
	}
}
